package visualcbir.util;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

/**
 *
 * @author sheila
 */
public class ComponentUtils {

    /**
     * It seems to be replaced by dlg.setLocationRelativeTo(Principal.getInstance());.  test that
     * @param componentInside
     * @param componentOutside
     */
    public static void setLocationAtCenter(Component componentInside,Component componentOutside ){
      Dimension componentSize = componentInside.getPreferredSize();
      Dimension cpnOut = componentOutside.getSize();
      //Point loc = getLocation();
      componentInside.setLocation((cpnOut.width - componentSize.width) / 2 , (cpnOut.height - componentSize.height) / 2 );
    }

    /**
     * Centers the JScrollPane content by centering the JScrollBar block horizontally
     * and vertically.
     * @param oJScrollPane
     */
    public static void centerScrollBars(JScrollPane oJScrollPane){
        JScrollBar verticalScrollBar = oJScrollPane.getVerticalScrollBar();
        JScrollBar horizontalScrollBar = oJScrollPane.getHorizontalScrollBar();
        verticalScrollBar.setValue(Math.round(verticalScrollBar.getMaximum()/2) - Math.round(verticalScrollBar.getBounds().height/2) );
        horizontalScrollBar.setValue( Math.round(horizontalScrollBar.getMaximum()/2)   - Math.round(horizontalScrollBar.getBounds().width/2));
    }

    /**
     * Centers the view port for a component
     * @param dimReal: real dimension of the component. This dimension >= dimAvailable.
     * @param dimAvailable: dimension of the visible part of the component.
     * @param jviewport: jviewport where the component is located.
     */
    public static void centerJViewPort(Dimension dimReal, Dimension dimAvailable, JViewport jviewport){
        int xpoint=(dimReal.width == dimAvailable.width) ? 0 : Math.round( (dimReal.width - dimAvailable.width)/2 );
        int ypoint=(dimReal.height== dimAvailable.height)? 0 : Math.round( (dimReal.height - dimAvailable.height)/2 );
        Point jViewportPoint = new Point (  xpoint, ypoint );
        jviewport.setViewPosition(jViewportPoint);
    }

    public static void printDimensions(String componentName, Dimension oDimension){
        System.out.println(" * "+componentName+" Dimension--> "+ oDimension.toString() );
    }

    /**
     * This function compute the correct Dimension of the given component in
     * order to envolve the view completely.
     * If oComponent < oView, the returned Dimension will adquire the oView Size.
     * Otherwise, the returned Dimension will be same as oComponent Dimension
     * If only horizontal or vertical from oView is bigger than oComponent
     * horizontal\verticals; then the returned dimension will have
     * that part bigger.
     *
     * dimCurrent is the visible part of the panel but that leads to screen shaking while resizing.
     * To avoid this problem we consider only the total component size, but it wont be correctly resized
     * when the structure gets smaller because the oComponent will keep its bigger size.
     * @param oComponent
     * @param oView
     * @return a new Dimension greater or equal than the oView Dimension
     */
    public static Dimension recomputeComponentDimensionsByView(JComponent oComponent, Dimension dimView){
        boolean change=false;
        //ComponentUtils.printDimensions("component",oComponent.getSize());

        //Dimension dimCurrent = oComponent.getVisibleRect().getSize();
        Dimension dimCurrent = oComponent.getSize();// less computations
        int newDimCurrentWidth=dimCurrent.width;
        int newDimCurrentHeight = dimCurrent.height;
        
        if (dimView.width > dimCurrent.width ){
            newDimCurrentWidth = dimView.width;
            change = true;
        }
        if (dimView.height > dimCurrent.height ){
            newDimCurrentHeight = dimView.height;
            change=true;
        }
        Dimension newDimCurrent = new Dimension(newDimCurrentWidth, newDimCurrentHeight);
        if (change) { //System.out.println("****************************** View is bigger that the actual component");
            oComponent.setSize(newDimCurrent);            
            //Let the scroll pane know to update itself and its scrollbars.
            oComponent.setPreferredSize(newDimCurrent); //ao fazer isto o conteudo se estica.
            oComponent.revalidate();// Contets GUI update. This will be enough to the parent scroll update by itself
            //ComponentUtils.printDimensions("new Component dimension",oComponent.getSize());
        }
        return newDimCurrent;
    }

}
